/*
 * GuiDisplayChangeState.java
 *
 * Created on 12 May 2003, 07:09
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui;

import com.velocityme.client.gui.calendar.CalendarDatePanel;
import com.velocityme.client.gui.calendar.CalendarTimePanel;
import com.velocityme.client.gui.exception.ExceptionWindow;
import javax.ejb.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import java.awt.Component;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.swing.JTabbedPane;
import com.velocityme.client.gui.exception.*;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.InvalidTimeException;
import java.text.ParseException;
import javax.swing.JOptionPane;

/**
 *
 * @author  Robert Crida Work
 */
public class GuiDisplayChangeState extends javax.swing.JPanel {
    
    private TaskPK m_taskPK;
    private KeySession m_key;
    private CalendarDatePanel m_datePanel;
    private CalendarTimePanel m_startTimePanel;
    private CalendarTimePanel m_stopTimePanel;
    private StateValue m_sourceStateValue;
    
    /** Creates new form GuiDisplayChangeState */
    public GuiDisplayChangeState(KeySession p_key, TaskPK p_taskPK) {
        initComponents();
        m_key = p_key;
        m_taskPK = p_taskPK;
        
        m_datePanel = new CalendarDatePanel();
        m_startTimePanel = new CalendarTimePanel();
        m_stopTimePanel = new CalendarTimePanel();
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(m_datePanel, gridBagConstraints);
        gridBagConstraints.gridx = 3;
        jPanel1.add(m_startTimePanel, gridBagConstraints);
        gridBagConstraints.gridx = 5;
        jPanel1.add(m_stopTimePanel, gridBagConstraints);
        Date startTime = new Date();
        boolean permissionTimeCreate = false;

        try {
            TaskValue taskValue = StatelessRemoteClientSessionUtil.getSession().getTaskValue(m_key, m_taskPK);
            jTextFieldTask.setText(StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, new NodePK(m_taskPK.getTaskId())));
            Collection transitions = StatelessRemoteClientSessionUtil.getSession().getTaskLegalTransitionValues(m_key, m_taskPK);
            Iterator i = transitions.iterator();
            while(i.hasNext()) {
                TransitionValue transitionValue = (TransitionValue)i.next();
                jComboBoxTransition.addItem(transitionValue);
                m_sourceStateValue = transitionValue.getSourceStateValue();
            }
            jSliderPercentageComplete.setValue((int)taskValue.getPercentageComplete().byteValue());
            jCheckBoxAssignTime.setSelected(m_sourceStateValue.getIsActive().booleanValue());
            permissionTimeCreate = StatelessRemoteClientSessionUtil.getSession().getAssignedPermissions(m_key, new NodePK(m_taskPK.getTaskId())).contains(new PermissionPK(new Integer(PermissionBean.TIME_CREATE)));
            startTime = StatelessRemoteClientSessionUtil.getSession().getTaskLastStateChangeTime(m_key, m_taskPK);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
        Date today = new Date();
        m_datePanel.setDate(today);
        m_startTimePanel.setDate(startTime);
        m_stopTimePanel.setDate(today);
        
        if (!permissionTimeCreate) {
            jCheckBoxAssignTime.setSelected(false);
            jCheckBoxAssignTime.setEnabled(false);
            m_datePanel.setEnabled(false);
            m_startTimePanel.setEnabled(false);
            m_stopTimePanel.setEnabled(false);
        }
    }
    
    public TimeValue getTimeValue() throws ParseException {
        TimeValue timeValue = new TimeValue();
        timeValue.setDescription(jTextAreaDescription.getText());
        timeValue.setStateValue(m_sourceStateValue);
        Date date = m_datePanel.getDate();
        Date startTime = m_startTimePanel.getDate();
        Date stopTime = m_stopTimePanel.getDate();
        date.setHours(startTime.getHours());
        date.setMinutes(startTime.getMinutes());
        timeValue.setStartTime(date);
        date = (Date)date.clone();
        date.setHours(stopTime.getHours());
        date.setMinutes(stopTime.getMinutes());
        timeValue.setStopTime(date);
        return timeValue;
    }
    
/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel2 = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabelTask = new javax.swing.JLabel();
        jTextFieldTask = new javax.swing.JTextField();
        jLabelTransition = new javax.swing.JLabel();
        jComboBoxTransition = new javax.swing.JComboBox();
        jLabelDescription = new javax.swing.JLabel();
        jScrollPaneDescription = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jLabelPercentageComplete = new javax.swing.JLabel();
        jSliderPercentageComplete = new javax.swing.JSlider();
        jLabelAssignTime = new javax.swing.JLabel();
        jCheckBoxAssignTime = new javax.swing.JCheckBox();
        jLabelDate = new javax.swing.JLabel();
        jLabelStart = new javax.swing.JLabel();
        jLabelStop = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
        jLabelTask.setText("Task:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jLabelTask, gridBagConstraints);

        jTextFieldTask.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(jTextFieldTask, gridBagConstraints);

        jLabelTransition.setText("Transition:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jLabelTransition, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(jComboBoxTransition, gridBagConstraints);

        jLabelDescription.setText("Description:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jLabelDescription, gridBagConstraints);

        jScrollPaneDescription.setPreferredSize(new java.awt.Dimension(106, 66));
        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPaneDescription.setViewportView(jTextAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jScrollPaneDescription, gridBagConstraints);

        jLabelPercentageComplete.setText("% Complete:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jLabelPercentageComplete, gridBagConstraints);

        jSliderPercentageComplete.setMajorTickSpacing(10);
        jSliderPercentageComplete.setPaintLabels(true);
        jSliderPercentageComplete.setPaintTicks(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jSliderPercentageComplete, gridBagConstraints);

        jLabelAssignTime.setText("Assign Time:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jLabelAssignTime, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(jCheckBoxAssignTime, gridBagConstraints);

        jLabelDate.setText("Date:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jLabelDate, gridBagConstraints);

        jLabelStart.setText("Start:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        jPanel1.add(jLabelStart, gridBagConstraints);

        jLabelStop.setText("Stop:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jLabelStop, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.NORTH);

    }//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);                
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        TransitionValue transitionValue = (TransitionValue)jComboBoxTransition.getSelectedItem();
        Byte percentageComplete = new Byte((byte)jSliderPercentageComplete.getValue());
        try {
            if (jCheckBoxAssignTime.isSelected()) {
                StatelessRemoteClientSessionUtil.getSession().taskTimeAssign(m_key, m_taskPK, percentageComplete, getTimeValue());
            }
            StatelessRemoteClientSessionUtil.getSession().taskChangeState(m_key, m_taskPK, transitionValue.getPrimaryKey(), jTextAreaDescription.getText(), percentageComplete, null, null, null);
            getParent().remove(this);
        }
        catch(InvalidTimeException e) {
            JOptionPane.showMessageDialog(this, "Start time must be before stop time.", "Commit failed", JOptionPane.WARNING_MESSAGE);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
    }//GEN-LAST:event_jButtonOKActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JCheckBox jCheckBoxAssignTime;
    private javax.swing.JComboBox jComboBoxTransition;
    private javax.swing.JLabel jLabelAssignTime;
    private javax.swing.JLabel jLabelDate;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelPercentageComplete;
    private javax.swing.JLabel jLabelStart;
    private javax.swing.JLabel jLabelStop;
    private javax.swing.JLabel jLabelTask;
    private javax.swing.JLabel jLabelTransition;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPaneDescription;
    private javax.swing.JSlider jSliderPercentageComplete;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldTask;
    // End of variables declaration//GEN-END:variables
    
}
